<?php

namespace App\Libs;

trait GetTableDataTrait
{
    //当datatable控件没启用排序时，可传入$orderby参数指定默认排序
    //当datatable控件启用排序时，$orderby参数传 null
    public function getTableData($para, $with, $orderby, $where) {
        $draw = $para['draw'];
        $search = $para['search']['value'];
        $order = isset($para['order']) ? $para['order'] : null;
        // $orderBy = ['telephone','asc'];
        $this_orWhere_orderBy = $this;

        if (isset($where)) {
            // $recordsTotal = $this->where($where[0], $where[1], $where[2])->count(); //------------sql 执行
            $recordsTotal = $this->where($where)->count(); //------------sql 执行
        }
        else {
            $recordsTotal = $this->count(); //------------sql 执行
        }

        $para_col = $para['columns'];
        if (strlen($search) > 0) {
            //判断是否有搜索条件
            //以下匿名函数实现的功能，laravel手册中叫参数分组
            $this_orWhere_orderBy = $this_orWhere_orderBy->where(function ($query) use ($search, $para_col) {
                foreach ($para_col as $val) {
                    if ($val['searchable'] === 'true') {
                        $query->orWhere($val['data'], 'like', '%' . $this->escape_like_str($search) . '%');
                    }
                }
            });
        }

        if (isset($where)) {
            // $this_orWhere_orderBy = $this_orWhere_orderBy->where($where[0], $where[1], $where[2]);
            $this_orWhere_orderBy = $this_orWhere_orderBy->where($where);
        }

        $recordsFiltered = strlen($search) > 0 ? $this_orWhere_orderBy->count() : $recordsTotal; //------------sql 执行

        if (isset($order)) {
            //判断是否启用排序
            foreach ($order as $val) {
                $this_orWhere_orderBy = $this_orWhere_orderBy->orderBy($para['columns'][intval($val['column'])]['data'], $val['dir']);
            }
        }
        else if (isset($orderby)) {
            //判断是否指定了第三个参数
            foreach ($orderby as $item) {
                if (is_array($item)) {
                    $this_orWhere_orderBy = $this_orWhere_orderBy->orderBy($item[0], $item[1]);
                }
                else {
                    $this_orWhere_orderBy = $this_orWhere_orderBy->orderBy($orderby[0], $orderby[1]);
                }
            }
        }

        if (isset($para['start'])) {
            $this_orWhere_orderBy = $this_orWhere_orderBy->offset($para['start']);
        }

        if (isset($para['length'])) {
            $this_orWhere_orderBy = $this_orWhere_orderBy->limit($para['length']);
        }

        if ($with) {
            # code...
            $data = $this_orWhere_orderBy->with($with)->get()->each(function ($item){
                $item['telephone']=substr($item['telephone'],0,3).'****'.substr($item['telephone'],7,11);
            }); //------------sql 执行
        } else {
            # code...
            $data = $this_orWhere_orderBy->get()->each(function ($item){
                $item['telephone']=substr($item['telephone'],0,3).'****'.substr($item['telephone'],7,11);
            });; //------------sql 执行
        }

        $result = ["draw" => intval($draw), "recordsTotal" => intval($recordsTotal), "recordsFiltered" => intval($recordsFiltered), "data" => $data,];
        return $result;
    }

    //datatable控件只把 单引号 进行了转义，“ \ _ % ” 需要自己去转义
    private function escape_like_str($str) {
        $like_escape_char = "\\";
        $appear_count = substr_count($str, $like_escape_char);
        // dd(str_repeat($like_escape_char, $appear_count));
        return str_replace([str_repeat($like_escape_char, $appear_count), '%', '_'], [str_repeat($like_escape_char, $appear_count * 2 + 1), $like_escape_char . '%', $like_escape_char . '_',], $str);
    }
}
